﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZhaoPin.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];

        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
            Roles.AddUserToRole(RegisterUser.UserName, "users");
            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            RecruitContainer ec = new RecruitContainer();
            TextBox tbRealName = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("tbRealName");
            Person p = Person.CreatePerson((Guid)Membership.GetUser(RegisterUser.UserName).ProviderUserKey, RegisterUser.UserName, tbRealName.Text, 0);
            ec.People.AddObject(p);
            Photo photo = Photo.CreatePhoto(p.Id);
            ec.Photos.AddObject(photo);
            ActivityLog log=ActivityLog.CreateActivityLog(Guid.Empty, p.Id, "注册", DateTime.Now);
            log.Details = GetClientIP();
            ec.ActivityLogs.AddObject(log);
            ec.SaveChanges();
            Response.Redirect(continueUrl);
        }

        protected string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }
    }
}
